import { Component, OnInit } from '@angular/core';
import { ServiceAlterService } from "./service-alter.service";
import { PaginationModel } from 'app/widgets/pagination';
import { environment } from '../../../../environments/environment';
import { DataService } from '../../../services/data.service';
import * as _ from 'lodash';

@Component({
  selector: 'app-service-alter',
  templateUrl: './service-alter.component.html',
  styleUrls: ['./service-alter.component.scss'],
  providers: [ServiceAlterService]
})
export class ServiceAlterComponent implements OnInit {
  // lodash
  public _: any = _;

  // 菜单数据
  public menuData: any;
  // 菜单权限set
  public menuResourceSet: any = [];
  //审核意见
  public sAuditOpinion: any;
  //审核修改项数据
  public changeCheckInfo: any = [];
  // 审核列表数据
  public checkList: any;
  // 审核列表全选
  public isCheckedAll: any = false;
  // 审核列表请求体
  public getCheckPost = {
    pageIndex: 1,
    pageSize: 10,
    sFundCode: '',
    sFullName: '',
    sAuditStatus: ''
  };
  // 审核列表页配置
  public checkPageInfo: PaginationModel = {
    currentPageNum: 1,
    pageSize: 10,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  };

  constructor(
    public serviceAlterService: ServiceAlterService,
    public dataService: DataService,
  ) { }

  ngOnInit() {
    let that = this;
    this.getCheckList();
    // 权限处理
    this.dataService.initTree.subscribe(data => {
      if (data) {
        let SessionMenuData = sessionStorage.getItem('menu');
        this.menuData = JSON.parse(SessionMenuData);
        this.menuResourceSet = [];
        _.forEach(this.menuData, (item, index) => {
          if (item.sName == '条目管理') {
            _.forEach(item.children, (it, idx) => {
              if (it.sName == '服务类条目变更审核') {
                that.menuResourceSet = it.resourceSet;
                sessionStorage.setItem('btn-entry', this.menuResourceSet);
              }
            })
          }
        })
      }
    })
    let SessionMenuData = sessionStorage.getItem('menu');
    this.menuData = JSON.parse(SessionMenuData);
    this.menuResourceSet = [];
    _.forEach(this.menuData, (item, index) => {
      if (item.sName == '条目管理') {
        _.forEach(item.children, (it, idx) => {
          if (it.sName == '服务类条目变更审核') {
            that.menuResourceSet = it.resourceSet;
            sessionStorage.setItem('btn-entry', this.menuResourceSet);
          }
        })
      }
    })
  }
  ngAfterViewInit() {

  }
  // 审核列表
  getCheckList(pageNum?, pageSize?) {
    let that = this;
    this.isCheckedAll = false;
    this.getCheckPost.sFundCode = _.trim(this.getCheckPost.sFundCode);
    this.getCheckPost.sFullName = _.trim(this.getCheckPost.sFullName);
    this.getCheckPost.pageIndex = pageNum ? pageNum : this.checkPageInfo.currentPageNum;
    this.getCheckPost.pageSize = pageSize ? pageSize : this.checkPageInfo.pageSize;
    this.serviceAlterService.getCheckList(this.getCheckPost).subscribe(data => {
      if (data) {
        // console.log(data)
        that.checkList = data.list;
        that.checkPageInfo.currentPageNum = data.pageNum;
        that.checkPageInfo.totalPages = data.pages;
        that.checkPageInfo.total = data.total;
        that.checkPageInfo.startRow = data.startRow;
        that.checkPageInfo.endRow = data.endRow;
      }
    });
  }
  // 审核个数分页
  checkpageSizeChange(pageSize: number) {
    if (pageSize !== this.checkPageInfo.pageSize) {
      this.checkPageInfo.pageSize = pageSize;
      this.getCheckList();
    }
  }
  // 审核条数分页
  checkPageNavigation(currentPageNum: number) {
    this.checkPageInfo.currentPageNum = currentPageNum;
    this.getCheckList(this.checkPageInfo.currentPageNum, this.checkPageInfo.pageSize);
  }
  // 审核列表全选
  checkedAll() {
    if (this.isCheckedAll) {
      _.forEach(this.checkList, item => {
        item.isChecked = true;
      });
    } else {
      _.forEach(this.checkList, item => {
        item.isChecked = false;
      });
    }
  }
  // 审核列表是否全选
  ctrlCheckAll() {
    let that = this;
    let checkList = [];
    _.forEach(this.checkList, item => {
      if (item.isChecked == true) {
        checkList.push(item);
      }
    })
    if (checkList.length == this.checkList.length) {
      this.isCheckedAll = true;
    } else {
      this.isCheckedAll = false;
    }
  }
  // 审核模版
  showCheck() {
    window['$']('#checkModel').modal('show');
  }
  // 导出
  exportExcel() {
    let that = this;
    let checkedItem = [];
    _.forEach(this.checkList, item => {
      if (item.isChecked) {
        checkedItem.push(item.nItemAuditNo);
      }
    })
    let converItem: any = checkedItem.join(',');
    let getBody = {
      "nItemAuditNoList": converItem,
      "sFundCode": this.getCheckPost.sFundCode,
      "sFullName": this.getCheckPost.sFullName,
      "sAuditStatus": this.getCheckPost.sAuditStatus
    }
    const iframe = window['$']("<iframe id='downloadiframe'>");
    iframe.attr('style', 'display:none');
    //  转码，兼容ie
    getBody.sFullName = encodeURI(getBody.sFullName);
    iframe.attr('src', environment.server + '/pms/v1/item/serItemAudit/export/serItemAudit?token=' + sessionStorage.getItem('token') + '&nItemAuditNoList=' + getBody.nItemAuditNoList + '&sFundCode=' + getBody.sFundCode + '&sFullName=' + getBody.sFullName + '&sAuditStatus=' + getBody.sAuditStatus);
    window['$']('body').append(iframe);
    setTimeout("$('#downloadiframe').remove()", 60000);
  }
  //查询-审核服务条目信息
  getServiceEntryInfo() {
    let that = this;
    let checkedItem = [];
    this.changeCheckInfo = [];
    _.forEach(this.checkList, item => {
      if (item.isChecked) {
        checkedItem.push(item);
      }
    })
    if (checkedItem.length < 1) {
      window['swal']("注意", "请选择一条记录进行审核!", "warning")
    } else if (checkedItem.length > 1) {
      window['swal']("注意", "只能选择一条记录进行审核!", "warning")
    } else {
      if (checkedItem[0].sAuditStatus == '0') {
        that.serviceAlterService.getServiceEntryInfo(checkedItem[0].nItemAuditNo).subscribe(data => {
          if (data) {
            that.changeCheckInfo.push(data);
            // console.log(that.changeCheckInfo)
            that.sAuditOpinion = that.changeCheckInfo[0].sAuditOpinion;
            window['$']('#checkModel').modal('show');
          }
        })
      } else {
        window['swal']("注意", "不能选择审核不通过或者审核通过的记录!", "warning")
      }

    }

  }
  //更新-服务条目-审核通过-不通过
  checkService(sAuditStatus) {
    let that = this;
    let putBody: any = this.changeCheckInfo[0]
    if (this.sAuditOpinion == '' || this.sAuditOpinion == undefined) {
      window['swal']("提示", "审核意见不能为空!", "warning");
    } else {
      window["swal"]({
        title: "注意",
        text: "是否确定进行审核",
        type: "warning",
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        showCancelButton: true,
        closeOnConfirm: false,
        closeOnCancel: true,
        showLoaderOnConfirm: true
      },
        function (isConfirm) {
          if (isConfirm) {
            putBody.sAuditOpinion = that.sAuditOpinion;
            that.serviceAlterService.checkService(putBody, sAuditStatus).subscribe(data => {
              if (data) {
                window['swal']("提示", "审核成功!", "success")
                that.getCheckList();
                window['$']('#checkModel').modal('hide');
              }
            })
          }
        })
    }
  }
  // 点击表格头排序
  orderData(colName) {
    let listDataAsc = _.orderBy(this.checkList, [colName], ["asc"]);
    if (JSON.stringify(this.checkList) == JSON.stringify(listDataAsc)) {
      listDataAsc = _.orderBy(this.checkList, [colName], ["desc"]);
    }
    this.checkList = listDataAsc;
  }
}
